﻿using Liu_Project.Dal.Admin;
using LSL.SqlClient;
using System.Data;
using Liu_Project.Utils;

namespace Liu_Project.Web.Admin.Handler.BaseHand
{
    /// <summary>
    /// UserRoleHand 的摘要说明
    /// </summary>
    public class UserRoleHand : HandlerBaseAsync
    {

        readonly UserRoleDal dal = new UserRoleDal();

        public void LoadPage()
        {
            _logOperation = "用户管理-获取列表数据";
            var sqlWhere = ExpressionMethod.GetSqlWhere(_context);

            var data = dal.GetUserList(sqlWhere, "UserId", "DESC", page, pageSize, ref total);

            _context.Response.Write(CommonFunction.SuccessJson(data, total));
        }

        public void QueryById()
        {
            _logOperation = "用户管理-查看";
            var data = dal.GetEntityById<Entity.Admin.EntUser>(CurrentUser.UserId.ToString());
            data.Password = "";
            _context.Response.Write(CommonFunction.SuccessJson(data));
        }

        public void Save()
        {
            _logOperation = "用户管理-保存";
            var _state = DataConvert.ToString(_context.Request["_state"]);
            var oldPwd = DataConvert.ToString(_context.Request["OldPwd"]);
            var Data = DataConvert.ToString(_context.Request["Data"], "0");
            var roleData = DataConvert.ToString(_context.Request["roleData"], "[]");
            var user = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.Admin.EntUser>(Data);
            var roleTable = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(roleData);

            dal.Save(_state, oldPwd, user, roleTable);
            if (_state == ActionOptions.Edit && user.UserId == CurrentUser.UserId)
            {
                var newUser = dal.GetEntityById<Entity.Admin.EntUser>(user.UserId.ToString());
                _context.Session["CurrentUser"] = newUser;
            }

            _context.Response.Write(CommonFunction.SuccessJson());
        }

        public void Delete()
        {
            _logOperation = "用户管理-删除";
            var Data = DataConvert.ToString(_context.Request["Data"], "0");

            new SQLHelper().ExecuteNonQuery("DELETE FROM EntUser WHERE UserId IN (" + Data + ")");

            _context.Response.Write(CommonFunction.SuccessJson());
        }

        public void GetLoadPages()
        {
            _logOperation = "登陆日志-获取列表数据";
            var sqlWhere = ExpressionMethod.GetSqlWhere(_context);

            var data = dal.GetUserLoginList(sqlWhere, "LoginTime", "DESC", page, pageSize, ref total);

            _context.Response.Write(CommonFunction.SuccessJson(data, total));
        }

        public void GetUserLogList()
        {
            _logOperation = "操作日志-获取列表数据";
            var sqlWhere = ExpressionMethod.GetSqlWhere(_context);

            var data = dal.GetUserLogList(sqlWhere, "LogTime", "DESC", page, pageSize, ref total);

            _context.Response.Write(CommonFunction.SuccessJson(data, total));
        }

    }
}